﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCM.UI.ChildWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCM.UI.Utils;
using System.Windows.Browser;

namespace Baosight.ZNH.ZMCM.UI
{
    public partial class ZMCMF2 : EFViewCmes
    {
        public ZMCMF2()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 列表中修改、删除前设置属性、参数
        /// 传递到后台代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.Grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 点击主表记录，显示从表相应记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Grid_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            if (null != this.Grid.SelectedItem)
            {
                EIRow _row = (this.Grid.SelectedItem as UIBaseEntity).GetEIRow();

                Dictionary<string, string> args = new Dictionary<string, string>();
                args.Add("fdReceiveId", _row["fdKeyId"].ToString());
                args.Add("fdIsfromPlan", _row["fdIsfromPlan"].ToString());
                this.container.LoadView<EFCustomViewContainerPanel>("ZMCMF3", args);
            }
            else
            {
                this.container.Content = null;
            }
        }

        /// <summary>
        /// 点击新增按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            EIInfo info = new EIInfo();
            info.Set(EIConstants.ServiceNameKey, "ZMCMF2");
            info.Set(EIConstants.MethodNameKey, "querySendGoodsNoteCode");
            this.Submit(info, QueryNoteCodeCompleted);
        }

        /// <summary>
        /// 点击作废按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnInvalidButtonClick(object sender, RoutedEventArgs e)
        {
            base.OnModifyButtonClick("ZMCMF0", "invalid", "作废", sender, e);
        }

        /// <summary>
        /// 点击从采购计划中提取数据按钮跳转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnStockInsertButtonClick(object sender, RoutedEventArgs e)
        {
            //this.Shell.LoadView("ZMCMF0");
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            EIInfo info = new EIInfo();
            info.Set(EIConstants.ServiceNameKey, "ZMCMF2");
            info.Set(EIConstants.MethodNameKey, "query");
            this.Submit(info, QueryStockDataCompleted);
        }

        /// <summary>
        /// 打印入库单操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnPrintButtonClick(object sender, RoutedEventArgs e)
        {
            //按钮失效
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;
            int count = this.Grid.GetCheckedEIRows().Count;
            if (count <= 0)
            {
                this.OpenEFWindowAlert("请选择一条记录进行打印！", true);
                return;
            }
            else if (count != 1)
            {
                this.OpenEFWindowAlert("只能选择一条记录进行打印！", true);
                return;
            }
            string fdKeyId = "'" + this.Grid.GetCheckedEIRows()[0]["fdKeyId"].ToString()+"'";
            Uri uri = Uploader.ConstructAbsoluteUri("../admin/RunmaxReport.jsp?projectName=CMESReport&reportName=ReceiveGoods&KeyId=" + fdKeyId);
            HtmlPage.Window.Navigate(uri, "_blank");
            base.TempButtonOpen(true);//按钮生效
        }


        /// <summary>
        /// 查询送货单的回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void QueryNoteCodeCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status != -1)
            {
                base.OnInsertButtonClickPopup(e.EIInfo, "到货登记-新增", "insert", null, TempButton, null);
            }
            else
            {
                base.OpenEFWindowAlert("查询采购数据失败！", true);
            }
        }

        /// <summary>
        /// 查询采购计划中的数据的回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void QueryStockDataCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status != -1)
            {
                EIInfo info = new EIInfo();
                EIBlock block = info.AddBlock("subBlock");
                block.SetColumns(e.EIInfo.GetBlock("result").GetColumns());
                block.SetRows(e.EIInfo.GetBlock("result").GetRows());
                info.SetBlock(e.EIInfo.GetBlock("cmes.cs.units"));
                info.Set("fdSendGoodsNoteCode", e.EIInfo.Get("fdSendGoodsNoteCode"));
                base.OnInsertButtonClickPopup(info, "到货登记-新增", "stockinsert", null, TempButton, null);
            }
            else
            {
                base.OpenEFWindowAlert("查询采购数据失败！", true);
            }
        }

        /// <summary>
        /// 批量入库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnBatchStorageButtonClick(object sender, RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            EIInfo info = new EIInfo();
            info.Set(EIConstants.ServiceNameKey, "ZMCMF2");
            info.Set(EIConstants.MethodNameKey, "queryReceiveGoods");
            this.Submit(info, QueryReceiveGoodsDataCompleted);
        }

        /// <summary>
        /// 查询到货登记中的数据的回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void QueryReceiveGoodsDataCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status != -1)
            {
                OpenReceiveGoodsPopup(e.EIInfo, "办理入库-选择物资", null, null);
            }
            else
            {
                base.OpenEFWindowAlert("查询到货数据失败！", true);
            }
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            ArrivalRegister view = new ArrivalRegister(_dialogType);//构造一个新的修改页面
            view.EIDataContext = new Platform.Core.UI.EIDataContext();
            _eIInfo.AddBlock((new MonthBlock()).GetMonth(true));
            view.EIDataContext.Update(_eIInfo);

            view.fdReason.Value = _eIInfo.Get("fdSendGoodsNoteCode").ToString();
            view.fdDate.Value = DateTime.Now.ToString("yyyy-MM-dd");

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = view;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(InsertWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 查询到货的数据传输
        /// 构造到货页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public void OpenReceiveGoodsPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            ReceiveGoodsBatchImport view = new ReceiveGoodsBatchImport();//构造一个新的修改页面
            view.EIDataContext = new Platform.Core.UI.EIDataContext();
            string noteCode = _eIInfo.Get("fdNoteCode").ToString();
            view.fdNoteDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
            view.fdDepotName.Value = _eIInfo.Get("depotName");
            view.fdNoteCode.Value = noteCode;
            (view.fdConfirmPeople.Content as CmesCustomView).Value = _eIInfo.Get("userId");
            (view.fdConfirmPeople.Content as CmesCustomView).Display = _eIInfo.Get("userName");
            _eIInfo.AddBlock((new StorageBlock()).GetMaterialSourceBlock(false));
            view.EIDataContext.Update(_eIInfo);

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = view;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(ReceiveGoodsWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }


        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InsertWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.Grid.Refresh();
            }

            //按钮生效
            base.TempButtonOpen(true);

            ArrivalRegister view = (sender as EFWindow).Content as ArrivalRegister;
            if (!view.InsertMeg.Equals(""))
            {
                this.OpenEFWindowAlert(view.InsertMeg, true);
            }
        }

        private void ReceiveGoodsWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.Grid.Refresh();
            }

            //按钮生效
            base.TempButtonOpen(true);
        }

        /// <summary>
        ///根据从表界面修改信息刷新页面
        /// </summary>
        public override void OnEIDataPublished(Platform.Core.EIInfo data)
        {
            if (null != this.Grid.SelectedItem)
            {
                EIInfo info = new EIInfo();
                info.Attributes.Add(EIConstants.ServiceNameKey, "ZMCMF0");
                info.Attributes.Add(EIConstants.MethodNameKey, "query");

                EIBlock block = info.AddBlock(EIConstants.QueryStatusBlock);

                EIColumn column = new EIColumn();
                column.Name = "fdKeyId";
                block.AddColumn(column);

                EIRow _row = (this.Grid.SelectedItem as UIBaseEntity).GetEIRow();
                EIRow row = new EIRow();
                row.Add("fdKeyId", _row["fdKeyId"]);
                block.AddRow(row);
                info.AddBlock(block);
                info.AddBlock(EIConstants.ResultBlock).Set(EIConstants.ShowCount, "true");
 
                IServiceAgent service = new HttpServiceAgent();
                service.CallService(info, OnCallChildServiceCompleted);

            }
        }

        /// <summary>
        /// 刷新主表数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnCallChildServiceCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo == null || e.EIInfo.GetStatus() == -1)
            {
            }
            else
            {
                EIRow row = (this.Grid.SelectedItem as UIBaseEntity).GetEIRow();
                row["fdNoImportCount"] = e.EIInfo.GetBlock(EIConstants.ResultBlock).Get(0, "fdNoImportCount");
                row["fdSum"] = e.EIInfo.GetBlock(EIConstants.ResultBlock).Get(0, "fdSum");
            }
        }
    }
}
